package com.yxf.coordinatorlayoutandbehavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class MyTextViewBehavior extends CoordinatorLayout.Behavior<TextView> {

    private static final String TAG = MyTextViewBehavior.class.getSimpleName();

    public MyTextViewBehavior() {

    }

    public MyTextViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) {
        if (child.getId() == R.id.child) {
            if (dependency.getId() == R.id.dependency) {
                return true;
            }
        }
        return super.layoutDependsOn(parent, child, dependency);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
        int leftMargin = parent.getWidth() - dependency.getRight();
        int topMargin = parent.getHeight() - dependency.getBottom();
        MainActivity.setViewLocationInCoordinatorLayout(leftMargin, topMargin, child);
        return true;
    }

    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, TextView child, View dependency) {
        super.onDependentViewRemoved(parent, child, dependency);
    }
}
